package org.exercise.schedule;

import java.util.TimerTask;

/**
 * @author Stranger
 * @version 1.0
 * @description: TODO
 * @date 2022/10/6 22:26
 */
public class ScheduleTask extends TimerTask {

    private Runnable command;
    private String name;

    private int count = 0;

    private int execCount;

    public ScheduleTask(Runnable command, String name, int execCount) {
        this.command = command;
        this.name = name;
        this.execCount = execCount;
    }

    public ScheduleTask(Runnable command, String name) {
        this.command = command;
        this.name = name;
    }

    public synchronized void run() {
        if (count > execCount - 1 && execCount != 0) {
            this.cancel();
            return;
        }
        this.command.run();
        count++;
        System.out.println(this.name + "执行" + this.count + "次");
    }

}
